<?php

/**
 * 上传图片
 * @param 表单字段名 $upfileinputname
 * @param 上传径路 $upfilepath
 * @param 上传最大 $upfilemax
 * @return unknown
 */


function mkdirm($path) { 
	if (!file_exists($path)){ 
		mkdirm(dirname($path)); 
		mkdir($path, 0777); 
	} 
} 

 
function upfile_check_extend($FilePath){
	$extend = pathinfo($FilePath);
	$extend = strtolower($extend["extension"]);
	return $extend;
}

function Uploadfiles($upfileinputname,$upfile_ext=false,$upfilepath="/uploads/files/",$upfilemax=100000){
			if($upfile_ext){
				$upfile_ext = strtr($upfile_ext,array('/'=>',','.'=>''));
			}else{
	        	$upfile_ext="rar,zip,jpg,jpeg,png,gif,swf,bmp,doc,docx,xls,pdf,chm,txt";
			}
			$extend_array=explode(",",$upfile_ext);
			$filename=$upfileinputname;
			$upfilepath=$upfilepath.date('Ym')."/";
			if(strtolower($_SERVER['REQUEST_METHOD'])=="post"){
				
				$ufile=$_FILES[$filename];
				$uploadmessage = false;
				switch($ufile['error']){
					case 1 : 
						//$uploadmessage = "上传文件大小超出限制";
						$uploadmessage = "文件大小不能超过10M";
						break;
					case 2 : 
						//$uploadmessage = "上传文件大小超出限制";
						$uploadmessage = "文件大小不能超过10M";
						break;
					case 3 : 
						//$uploadmessage = "文件仅被部分上传";
						$uploadmessage = "文件上传失败";
						break;
					case 4 : 
						//$uploadmessage = "没有文件被上传";
						$uploadmessage = false;
						break;
					case 5 : 
						//$uploadmessage = "找不到临时文件夹";
						$uploadmessage = false;
						break;
					case 6 : 
						//$uploadmessage = "文件写入失败";
						$uploadmessage = "文件上传失败,请检查配置权限";
						break;
				}
				if($uploadmessage){
					exit($uploadmessage);
				}
			
				//判断文件是否存在
				if(!is_uploaded_file($ufile["tmp_name"])){
					return array(false,'无效的文件');
			        break;
				}
				
				
				if(!in_array(upfile_check_extend($ufile["name"]),$extend_array)){
					return array(false,'文件类型不正确');
			        break;
				}
				
				//判断文件大小
				if(($upfilemax*1024)<$ufile["size"]){
					return array(false,'文件太大了');
			        break;
				}
				if(!file_exists(SITEROOT.$upfilepath))
			    {
					mkdirm(SITEROOT.$upfilepath);
			        //mkdir($upfilepath);
			    }
			    //获取扩展名 $ftype
			    $finfo=pathinfo($ufile["name"]);
			    $ftype=$finfo["extension"];
			    //获取图片宽高 
			   // $images_size=getimagesize($ufile["tmp_name"]);
				//$images_w=$images_size[0];
				//$images_h=$images_size[1];
				//保存是文件路径
				
				$R_string_path=$upfilepath.uniqid().".".$ftype;
				$destination=SITEROOT.$R_string_path;
				
				if(file_exists($destination))
			    {
			        return array(false,'同名文件已经存在了');
			        break;
			    }
			    if(!move_uploaded_file ($ufile["tmp_name"], $destination))
			    {
			        return array(false,'移动文件出错');
			        break;
			    }
			  	return $R_string_path;
			}
}
 
function upfilepic($upfileinputname,$upfilepath="/uploads/img/",$upfilemax=5000){
			$upfiletypes=array('image/jpg',  //上传文件类型列表
			'image/jpeg',
			'image/png',
			'image/pjpeg',
			'image/gif',
			'image/bmp',
			'image/x-png');
			$upfile_ext="jpg,jpeg,png,gif,bmp,webp";
			$extend_array=explode(",",$upfile_ext);
			$filename=$upfileinputname;
			$upfilepath=$upfilepath.date('Ym')."/";
			if(strtolower($_SERVER['REQUEST_METHOD'])=="post"){

				$ufile=$_FILES[$filename];
				//判断文件是否存在
				if(!is_uploaded_file($ufile["tmp_name"])){
					return array(false,'无效的文件');
			        break;
				}
				
				
				if(!in_array(upfile_check_extend($ufile["name"]),$extend_array)){
					return array(false,'文件类型不正确');
			        break;
				}
				
				//判断文件大小
				if(($upfilemax*1024)<$ufile["size"]){
					return array(false,'文件太大了');
			        break;
				}
				if(!file_exists(SITEROOT.$upfilepath))
			    {
					mkdirm(SITEROOT.$upfilepath);
			        //mkdir($upfilepath);
			    }
			    //获取扩展名 $ftype
			    $finfo=pathinfo($ufile["name"]);
			    $ftype=$finfo["extension"];
			    //获取图片宽高 
			   // $images_size=getimagesize($ufile["tmp_name"]);
				//$images_w=$images_size[0];
				//$images_h=$images_size[1];
				//保存是文件路径
				
				$R_string_path=$upfilepath.uniqid().".".$ftype;
				$destination=SITEROOT.$R_string_path;
				
				if(file_exists($destination))
			    {
			        return array(false,'同名文件已经存在了');
			        break;
			    }
			    if(!move_uploaded_file ($ufile["tmp_name"], $destination))
			    {
			        return array(false,'移动文件出错');
			        break;
			    }
			  	return $R_string_path;
			}
}

?>